import os
import pygame

from settings import SCREEN_WIDTH, SCREEN_HEIGHT, TILE_SIZE
from support import import_folder
class Overlay:
    def __init__(self,player):
        self.player = player
        self.display_surf = pygame.display.get_surface()

        self.level_surf = player.health
        self.import_assets()
        self.frame_index = 0

        self.image = self.animations['level_up'][self.frame_index]
        self.health_images = self.animations['health']  # 加载血条图片
        self.mp_images = self.animations['mp']  # 加载MP条图片
        self.health_rect = pygame.Rect(10, 10, 200, 20)  # 血条位置和大小
    

    def import_assets(self):
        self.animations = {'level_up':[],'health':[],'mp':[]}
        for animation in self.animations.keys():
            full_path = './graphics/character/' + animation
            self.animations[animation] = import_folder(full_path)
        print(self.animations)

    def display(self,tool_surf=None, seed_surf=None, player=None):
        # 显示血条
        if 0 <= self.player.health <= 7:  # 确保索引在有效范围内
            health_image = self.health_images[7-self.player.health]
            health_rect = health_image.get_rect(topleft=(10, 10))
            self.display_surf.blit(health_image, health_rect)
            
            # 显示HP文字
            font = pygame.font.Font(None, 24)  # 使用默认字体，大小为24
            hp_text = font.render(f'HP: {self.player.health}/{self.player.max_health}', True, (255, 255, 255))
            hp_rect = hp_text.get_rect(topleft=(10, 35))  # 放在血条下面
            self.display_surf.blit(hp_text, hp_rect)
            
        # 显示MP条
        if 0 <= self.player.mp <= 4:  # 确保索引在有效范围内
            mp_image = self.mp_images[4-self.player.mp]  # 使用与血条相同的索引计算方式
            mp_rect = mp_image.get_rect(topleft=(110, 20))  # 放在血条右侧
            self.display_surf.blit(mp_image, mp_rect)
            
            # 显示MP文字
            font = pygame.font.Font(None, 24)  # 使用默认字体，大小为24
            mp_text = font.render(f'MP: {self.player.mp}/{self.player.max_mp}', True, (255, 255, 255))
            mp_rect = mp_text.get_rect(topleft=(110, 35))  # 放在蓝条下面
            self.display_surf.blit(mp_text, mp_rect)
            
        # 显示分数
        screen_width = self.display_surf.get_width()
        font = pygame.font.Font(None, 36)  # 使用更大的字体显示分数
        score_text = font.render(f'Score: {self.player.score}', True, (255, 255, 255))
        score_rect = score_text.get_rect(midtop=(screen_width // 2, 10))  # 放在屏幕顶部中间
        self.display_surf.blit(score_text, score_rect)

        # 显示等级


        level_color = (255, 150, 0) if self.player.level >= self.player.max_level else (255, 255, 255)
        level_text = font.render(f'Level: {self.player.level}', True, level_color)
        level_rect = level_text.get_rect(topleft=(10, 60))
        self.display_surf.blit(level_text, level_rect)

        # 显示经验值进度条背景
        exp_bar_width = 200
        exp_bar_height = 20
        exp_bar_rect = pygame.Rect(10, 90, exp_bar_width, exp_bar_height)
        pygame.draw.rect(self.display_surf, (50, 50, 50), exp_bar_rect)  # 深灰色背景

        # 显示经验值进度条
        if self.player.level < self.player.max_level:
            try:
                exp_percent = min(1.0, self.player.exp / max(1, self.player.exp_to_next_level))
                current_exp_width = int(exp_bar_width * exp_percent)
                if current_exp_width > 0:
                    current_exp_rect = pygame.Rect(10, 90, current_exp_width, exp_bar_height)
                    pygame.draw.rect(self.display_surf, (0, 255, 255), current_exp_rect)  # 青色进度条
                    # 绘制进度条边框
                    pygame.draw.rect(self.display_surf, (255, 255, 255), exp_bar_rect, 2)  # 白色边框
            except Exception as e:
                print(f"经验值计算错误: {e}")
                print(f"当前经验值: {self.player.exp}")
                print(f"升级所需经验值: {self.player.exp_to_next_level}")

        # 显示经验值文字
        if self.player.level >= self.player.max_level:
            exp_text = font.render('MAX LEVEL', True, (255, 215, 0))  # 金色文字
        else:
            exp_percent = (self.player.exp / max(1, self.player.exp_to_next_level)) * 100
            exp_text = font.render(f'EXP: {self.player.exp}/{self.player.exp_to_next_level} ({exp_percent:.1f}%)', 
                                 True, (255, 255, 255))
        exp_text_rect = exp_text.get_rect(topleft=(10, 115))
        self.display_surf.blit(exp_text, exp_text_rect)